
package com.rt.schedulenew.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rt.schedulebase.dto.DictExamItemDto;
import com.rt.schedulebase.dto.DictScheduleApmRegularDto;
import com.rt.schedulebase.dto.RuleDto;
import com.rt.schedulebase.entity.DictItemGroup;
import com.rt.schedulebase.entity.DictItemGroupDetail;
import com.rt.schedulebase.entity.DictScheduleApmRegular;
import com.rt.schedulebase.entity.DictScheduleRegularDef;
import com.rt.schedulebase.mapper.DictItemGroupDetailMapper;
import com.rt.schedulebase.mapper.DictScheduleApmRegularMapper;
import com.rt.schedulebase.mapper.DictScheduleRegularDefMapper;
import com.rt.schedulenew.api.IDictScheduleApmRegularService;
import com.rt.schedulenew.utils.util.JsonUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DictScheduleApmRegularServiceImpl extends ServiceImpl<DictScheduleApmRegularMapper, DictScheduleApmRegular> implements IDictScheduleApmRegularService {
    @Autowired
    private DictItemGroupDetailMapper dictItemGroupDetailMapper;

    @Autowired
    private DictScheduleRegularDefMapper dictScheduleRegularDefMapper;

    @Override
    public List<DictScheduleApmRegular> selectList(DictScheduleApmRegularDto dto) {
        QueryWrapper<DictScheduleApmRegular> apmRegularQueryWrapper = new QueryWrapper<>();
        apmRegularQueryWrapper.eq("APM_CODE", dto.getApmCode());
        return baseMapper.selectList(apmRegularQueryWrapper);
    }

    @Override
    public List<DictScheduleApmRegular> selectForMaxLimitList(String apmCode, String regularType) {
        DictScheduleApmRegularDto dsar = new DictScheduleApmRegularDto();
        dsar.setApmCode(apmCode);
        dsar.setRegularType(regularType);
        return baseMapper.getScheduleRegular(dsar);
    }

    /**
     * 根据时段规则id 构建 时段预约规则 RuleDto
     *
     * @param regularId
     * @return
     */
    @Override
    public RuleDto getRuleDto(String regularId) {
        RuleDto ruleDto = new RuleDto();
        QueryWrapper<DictScheduleRegularDef> def = new QueryWrapper<>();
        def.select("REGULAR_TYPE", "EXAM_CLASS", "REGULAR_ID", "REGULAR_NAME", "REGULAR_MEMO", "SORT_NO", "REGULAR_CONTENT", "nvl(exclude_flag,0) excludeFlag");
        def.eq("REGULAR_ID", regularId);
        def.orderByDesc("nvl(exclude_flag,0)");
        DictScheduleRegularDef dictScheduleRegularDef = dictScheduleRegularDefMapper.selectOne(def);
        if (dictScheduleRegularDef != null) {
            String regularContent = dictScheduleRegularDef.getRegularContent();
            if (StringUtils.isNotBlank(regularContent)) {
                ruleDto = JsonUtil.toBean(regularContent, RuleDto.class);
                ruleDto.setExamClass(dictScheduleRegularDef.getExamClass());
                ruleDto.setRegularId(dictScheduleRegularDef.getRegularId());
                ruleDto.setRegularName(dictScheduleRegularDef.getRegularName());
                ruleDto.setRegularMemo(dictScheduleRegularDef.getRegularMemo());
                ruleDto.setSortNo(dictScheduleRegularDef.getSortNo());
                ruleDto.setExcludeFlag(dictScheduleRegularDef.getExcludeFlag());
                ruleDto.setRgularType(dictScheduleRegularDef.getRegularType());

                if (ruleDto.getExamItem() != null && CollectionUtils.isNotEmpty(ruleDto.getExamItem().getValue())) {
                    List<DictExamItemDto> itemDtoList = new ArrayList<>();
                    for (String itecode : ruleDto.getExamItem().getValue()) {
                        DictExamItemDto itemDto = new DictExamItemDto();
                        itemDto.setItemCode(itecode);
                        if (ruleDto.getSpecialItem() != null) {
                            RuleDto.LimitScope specialScope = ruleDto.getSpecialItem();
                            if (CollectionUtils.isNotEmpty(specialScope.getValue())
                                    && specialScope.getValue().contains(itecode)) {
                                itemDto.setIsSpecial(Boolean.TRUE);
                            }
                        }
                        if (ruleDto.getSpecialSingleItem() != null
                                && CollectionUtils.isNotEmpty(ruleDto.getSpecialSingleItem().getValue())) {
                            RuleDto.LimitScope specialScope = ruleDto.getSpecialSingleItem();
                            if (CollectionUtils.isNotEmpty(specialScope.getValue())
                                    && specialScope.getValue().contains(itecode)) {
                                itemDto.setIsSingleSpecial(Boolean.TRUE);
                            }
                        }
                        if (ruleDto.getSpecialMultiItem() != null
                                && CollectionUtils.isNotEmpty(ruleDto.getSpecialMultiItem().getValue())) {
                            RuleDto.LimitScope specialScope = ruleDto.getSpecialMultiItem();
                            if (CollectionUtils.isNotEmpty(specialScope.getValue())
                                    && specialScope.getValue().contains(itecode)) {
                                itemDto.setIsMultiSpecial(Boolean.TRUE);
                            }
                        }
                        itemDtoList.add(itemDto);
                    }
                    ruleDto.setDictExamItemList(itemDtoList);
                }

                if (ruleDto.getItemPackage() != null && CollectionUtils.isNotEmpty(ruleDto.getItemPackage().getValue())) {
                    QueryWrapper<DictItemGroupDetail> itemGroupDetailQueryWrapper = new QueryWrapper<>();
                    itemGroupDetailQueryWrapper.in("GROUP_CODE", ruleDto.getItemPackage().getValue());
                    List<DictItemGroupDetail> itemGroupDetails = dictItemGroupDetailMapper.selectList(itemGroupDetailQueryWrapper);
                    if (CollectionUtils.isNotEmpty(itemGroupDetails)) {
                        List<String> itemCodes = new ArrayList<>();
                        List<DictExamItemDto> itemDtoList2 = new ArrayList<>();

                        for (DictItemGroupDetail itemGroupDetail : itemGroupDetails) {
                            String itemCode = itemGroupDetail.getItemCode();
                            itemCodes.add(itemCode);

                            DictExamItemDto itemDto2 = new DictExamItemDto();
                            itemDto2.setItemCode(itemCode);
                            itemDto2.setIsPackage(Boolean.TRUE);
                            if (ruleDto.getSpecialItem() != null) {
                                RuleDto.LimitScope specialScope2 = ruleDto.getSpecialItem();
                                if (CollectionUtils.isNotEmpty(specialScope2.getValue()) && specialScope2.getValue().contains(itemCode)) {
                                    itemDto2.setIsSpecial(Boolean.TRUE);
                                }
                            }
                            // 标记为单个特殊
                            if (ruleDto.getSpecialSingleItem() != null
                                    && CollectionUtils.isNotEmpty(ruleDto.getSpecialSingleItem().getValue())) {
                                RuleDto.LimitScope specialScope = ruleDto.getSpecialSingleItem();
                                if (CollectionUtils.isNotEmpty(specialScope.getValue())
                                        && specialScope.getValue().contains(itemCode)) {
                                    itemDto2.setIsSingleSpecial(Boolean.TRUE);
                                }
                            }
                            itemDtoList2.add(itemDto2);
                        }
                        if (ruleDto.getDictExamItemList() != null) {
                            itemDtoList2.addAll(ruleDto.getDictExamItemList());
                        }
                        ruleDto.setDictExamItemList(itemDtoList2);

                        if (CollectionUtils.isNotEmpty(itemCodes)) {
                            if (ruleDto.getExamItem() != null) {
                                if (ruleDto.getExamItem().getValue() != null && !ruleDto.getExamItem().getValue().isEmpty()) {
                                    ruleDto.getExamItem().getValue().addAll(itemCodes);
                                } else {
                                    RuleDto.LimitScope itemScope = new RuleDto.LimitScope();
                                    itemScope.setGrant("1");
                                    itemScope.setValue(itemCodes);
                                    ruleDto.setExamItem(itemScope);
                                }
                            } else {
                                RuleDto.LimitScope itemScope = new RuleDto.LimitScope();
                                itemScope.setGrant("1");
                                itemScope.setValue(itemCodes);
                                ruleDto.setExamItem(itemScope);
                            }
                        }
                    }
                }


            }
        }
        return ruleDto;
    }

    @Override
    public List<DictScheduleApmRegularDto> selectRegularTypeGroupList(DictScheduleApmRegularDto dsarDto) {
        return baseMapper.selectRegularTypeGroupList(dsarDto);
    }

    @Override
    public void saveRegular(DictScheduleApmRegularDto apmRegularDto) {
        String apmCode = apmRegularDto.getApmCode();
        delete(apmRegularDto);
        String oldApmCode = apmRegularDto.getOldApmCode();
        apmRegularDto.setApmCode(oldApmCode);
        List<DictScheduleApmRegular> apmRegulars = selectList(apmRegularDto);
        if (apmRegulars != null && apmRegulars.size() > 0) {
            for (DictScheduleApmRegular apmRegular : apmRegulars) {
                apmRegular.setApmCode(apmCode);
                save(apmRegular);
            }
        }
    }

    @Override
    public void delete(DictScheduleApmRegularDto apmRegularDto) {
        if (StringUtils.isNotEmpty(apmRegularDto.getApmCode())) {
            QueryWrapper<DictScheduleApmRegular> wrapper = new QueryWrapper<>();
            wrapper.eq("APM_CODE", apmRegularDto.getApmCode());
            baseMapper.delete(wrapper);
        }
    }
}
